home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / cvs-1.8 / cvs-1 / cvs-1.8.1 / windows-NT / pwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-06  |  3.9 KB  |  210 lines

  1. /*  pwd.c - Try to approximate UN*X's getuser...() functions under MS-DOS.
  2.     Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.     $Header: /u/src/master/ccvs/windows-NT/pwd.c,v 1.2 1995/08/30 20:44:05 jimb Exp $
  19. */
  20.  
  21. /* This 'implementation' is conjectured from the use of this functions in
  22.    the RCS and BASH distributions.  Of course these functions don't do too
  23.    much useful things under MS-DOS, but using them avoids many "#ifdef
  24.    MSDOS" in ported UN*X code ...  */
  25.  
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <pwd.h>
  31.  
  32. char* win32getlogin();
  33. static char *lookup_env (char **);
  34.  
  35. /* where people might scribble their name into the environment ... */
  36.  
  37. static char *login_strings[] =
  38. {
  39.   "LOGIN", "USER", "MAILNAME", (char *) 0
  40. };
  41.  
  42. static char *group_strings[] =
  43. {
  44.   "GROUP", (char *) 0
  45. };
  46.  
  47.  
  48. static char *anonymous = "anonymous";    /* if all else fails ... */
  49.  
  50. static char *home_dir = ".";    /* we feel (no|every)where at home */
  51. static char *login_shell = "not command.com!";
  52.  
  53. static char *login = (char *) 0;/* cache the names here    */
  54. static char *group = (char *) 0;
  55.  
  56. static struct passwd pw;    /* should we return a malloc()'d structure   */
  57. static struct group gr;        /* instead of pointers to static structures? */
  58.  
  59. /* return something like a username in a (butchered!) passwd structure. */
  60. struct passwd *
  61. getpwuid (int uid)
  62. {
  63.   pw.pw_name = getlogin ();
  64.   pw.pw_dir = home_dir;
  65.   pw.pw_shell = login_shell;
  66.   pw.pw_uid = 0;
  67.  
  68.   return &pw;
  69. }
  70.  
  71. struct passwd *
  72. getpwnam (char *name)
  73. {
  74.   return (struct passwd *) 0;
  75. }
  76.  
  77. /* return something like a groupname in a (butchered!) group structure. */
  78. struct group *
  79. getgrgid (int uid)
  80. {
  81.   gr.gr_name = getgr_name ();
  82.   gr.gr_gid = 0;
  83.  
  84.   return &gr;
  85. }
  86.  
  87. struct group *
  88. getgrnam (char *name)
  89. {
  90.   return (struct group *) 0;
  91. }
  92.  
  93. /* return something like a username. */
  94. char *
  95. getlogin ()
  96. {
  97.   if (!login)
  98.      login = win32getlogin();
  99.  
  100.   if (!login)            /* have we been called before? */
  101.     login = lookup_env (login_strings);
  102.  
  103.   if (!login)            /* have we been successful? */
  104.     login = anonymous;
  105.  
  106.   return login;
  107. }
  108.  
  109. /* return something like a group.  */
  110. char *
  111. getgr_name ()
  112. {
  113.   if (!group)            /* have we been called before? */
  114.     group = lookup_env (group_strings);
  115.  
  116.   if (!group)            /* have we been successful? */
  117.     group = anonymous;
  118.  
  119.   return group;
  120. }
  121.  
  122. /* return something like a uid.  */
  123. int
  124. getuid ()
  125. {
  126.   return 0;            /* every user is a super user ... */
  127. }
  128.  
  129. int
  130. getgid ()
  131. {
  132.   return 0;
  133. }
  134.  
  135. int
  136. geteuid ()
  137. {
  138.   return 0;
  139. }
  140.  
  141. int
  142. getegid ()
  143. {
  144.   return 0;
  145. }
  146.  
  147. struct passwd *
  148. getpwent ()
  149. {
  150.   return (struct passwd *) 0;
  151. }
  152.  
  153. void
  154. setpwent ()
  155. {
  156. }
  157.  
  158. void
  159. endpwent ()
  160. {
  161. }
  162.  
  163. void
  164. endgrent ()
  165. {
  166. }
  167.  
  168. /* return groups.  */
  169. int
  170. getgroups (int ngroups, int *groups)
  171. {
  172.   *groups = 0;
  173.   return 1;
  174. }
  175.  
  176. /* lookup environment.  */
  177. static char *
  178. lookup_env (char *table[])
  179. {
  180.   char *ptr;
  181.   char *entry;
  182.   size_t len;
  183.  
  184.   while (*table && !(ptr = getenv (*table++))) ;    /* scan table */
  185.  
  186.   if (!ptr)
  187.     return (char *) 0;
  188.  
  189.   len = strcspn (ptr, " \n\t\n\r");    /* any WS?       */
  190.   if (!(entry = malloc (len + 1)))
  191.     {
  192.       fprintf (stderr, "Out of memory.\nStop.");
  193.       exit (-1);
  194.     }
  195.  
  196.   strncpy (entry, ptr, len);
  197.   entry[len] = '\0';
  198.  
  199.   return entry;
  200.  
  201. }
  202.  
  203. /*
  204.  * Local Variables:
  205.  * mode:C
  206.  * ChangeLog:ChangeLog
  207.  * compile-command:make
  208.  * End:
  209.  */
  210.